package com.service.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.service.common.core.domain.entity.ScoreList;
import com.service.system.mapper.ScoreListMapper;
import com.service.system.service.IScoreListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 分数信息Service业务层处理
 *
 * @author service
 * @date 2024-05-15
 */
@Service
public class ScoreListServiceImpl implements IScoreListService
{
    @Autowired
    private ScoreListMapper scoreListMapper;

    /**
     * 查询分数信息
     *
     * @param id 分数信息主键
     * @return 分数信息
     */
    @Override
    public ScoreList selectScoreListById(Long id)
    {
        return scoreListMapper.selectScoreListById(id);
    }

    /**
     * 查询分数信息列表
     *
     * @param scoreList 分数信息
     * @return 分数信息
     */
    @Override
    public List<ScoreList> selectScoreListList(ScoreList scoreList)
    {
        return scoreListMapper.selectScoreListList(scoreList);
    }

    /**
     * 新增分数信息
     *
     * @param scoreList 分数信息
     * @return 结果
     */
    @Override
    public int insertScoreList(ScoreList scoreList)
    {
        return scoreListMapper.insertScoreList(scoreList);
    }

    /**
     * 修改分数信息
     *
     * @param scoreList 分数信息
     * @return 结果
     */
    @Override
    public int updateScoreList(ScoreList scoreList)
    {
        return scoreListMapper.updateScoreList(scoreList);
    }

    /**
     * 批量删除分数信息
     *
     * @param ids 需要删除的分数信息主键
     * @return 结果
     */
    @Override
    public int deleteScoreListByIds(Long[] ids)
    {
        return scoreListMapper.deleteScoreListByIds(ids);
    }

    /**
     * 删除分数信息信息
     *
     * @param id 分数信息主键
     * @return 结果
     */
    @Override
    public int deleteScoreListById(Long id)
    {
        return scoreListMapper.deleteScoreListById(id);
    }

    @Override
    public List<ScoreList> selectScoreInfo(String id) {
        if (id==null||id.equals("undefined")){
            return null;
        }
        List<ScoreList> scoreLists = scoreListMapper.selectScoreInfo(id);
        scoreLists.forEach(x->{
            String[] score = x.getScore().split(";");
            String[] num1 = score[0].split(",");
            String[] num2 = score[1].split(",");
            String[] num3 = score[2].split(",");

            List<String> allNums = new ArrayList<>();
            Stream<String> stream = Stream.of(num1, num2, num3).flatMap(Arrays::stream);
            allNums = stream.collect(Collectors.toList());
            int sum = 0;
            for (String s : allNums) {
                int num = Integer.parseInt(s);
                sum += num;
            }
            BigDecimal number = new BigDecimal(sum/1.5).setScale(1, BigDecimal.ROUND_HALF_UP);
            x.setScore(String.valueOf(number));
        });


        return scoreLists;
    }
}
